package com.csu.letaobackend.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.csu.letaobackend.bean.Itemeval;
import com.csu.letaobackend.bean.Shop;
import com.csu.letaobackend.bean.User;
import com.csu.letaobackend.commom.CommonResponse;
import com.csu.letaobackend.service.EvaluationService;
import com.csu.letaobackend.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@CrossOrigin
@RequestMapping("/evaluation")
public class EvalutionController {

      @Autowired
      EvaluationService evaluationService;

      @GetMapping("/collections/items")
      @ResponseBody
      public CommonResponse<List<ItemVO>> getCollections(HttpSession httpSession) {
            User login_user = (User) httpSession.getAttribute("login_user");
            return evaluationService.getCollections(login_user.getUsername());
      }

      @GetMapping("/collections/pages/{pagesize}/{pageid}")
      @ResponseBody
      public CommonResponse<IPage<ItemVO>> getFavoriteItemsPage(@PathVariable int pagesize, @PathVariable int pageid, HttpSession httpSession) {
            User login_user = (User) httpSession.getAttribute("login_user");
            String username;
            if (login_user == null)
                  username = null;
            else
                  username = login_user.getUsername();
            return evaluationService.getCollectionsPages(pageid, pagesize, username);
      }

      @PostMapping("/collections/items/{itemid}")
      @ResponseBody
      CommonResponse<List<ItemVO>> addCollections(@PathVariable int itemid, HttpSession httpSession) {
            User login_user = (User) httpSession.getAttribute("login_user");
            return evaluationService.addCollections(login_user.getUsername(), itemid);
      }

      @DeleteMapping("/collections/items/{itemid}")
      @ResponseBody
      CommonResponse<List<ItemVO>> deleteCollections(@PathVariable int itemid, HttpSession httpSession) {
            User login_user = (User) httpSession.getAttribute("login_user");
            return evaluationService.deleteCollections(login_user.getUsername(), itemid);
      }


      @GetMapping("/collections/shops")
      @ResponseBody
      CommonResponse<List<Shop>> getCollectionShops(HttpSession httpSession) {
            User login_user = (User) httpSession.getAttribute("login_user");
            return evaluationService.getCollectionShops(login_user.getUsername());
      }


      @PostMapping("/collections/shops/{shopid}")
      @ResponseBody
      CommonResponse<List<Shop>> addCollectionsShops(@PathVariable int shopid, HttpSession httpSession) {
            User login_user = (User) httpSession.getAttribute("login_user");
            return evaluationService.addCollectionsShops(login_user.getUsername(), shopid);
      }


      @DeleteMapping("/collections/shops/{shopid}")
      @ResponseBody
      CommonResponse<List<Shop>> deleteCollectionsShops(@PathVariable int shopid, HttpSession httpSession) {
            User login_user = (User) httpSession.getAttribute("login_user");
            return evaluationService.deleteCollectionsShops(login_user.getUsername(), shopid);
      }


      @PostMapping("/items/{itemid}")
      @ResponseBody
      CommonResponse<List<Itemeval>> addEvaluations(@PathVariable int itemid, @RequestParam String content, @RequestParam double star, HttpSession httpSession) {
            User login_user = (User) httpSession.getAttribute("login_user");
            return evaluationService.addEvaluations(itemid, content, star, login_user.getUsername());
      }


      @GetMapping("/items/{itemid}")
      @ResponseBody
      CommonResponse<List<Itemeval>> getEvaluations(@PathVariable int itemid, HttpSession httpSession) {
            return evaluationService.getEvaluations(itemid);
      }

}
